package fr.umlv.galcon.ship.type;

import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import fr.umlv.galcon.item.GraphicItem;
import fr.umlv.galcon.ship.ShipAbstract;
import fr.umlv.galcon.ship.Squadron;
import fr.umlv.galcon.utils.Utils;

public class MediumShip extends ShipAbstract {

    private final Rectangle2D shape;

    /**
     * Constructor
     * 
     * @param x
     *            X coordinate of the Ship.
     * @param y
     *            Y coordinate of the Ship.
     * @param width
     *            Width of the Ship.
     * @param squadron
     *            Squadron which contains the Ship.
     */
    public MediumShip(int x, int y, int width, Squadron squadron) {
	super(x, y, width, squadron.getOriginPlanet().getShipProperties(), squadron);
	this.shape = new Rectangle2D.Double(x, y, width, width);
    }

    @Override
    public void draw(Graphics2D graphics) {
	double currentX = super.getCenter().getX();
	double currentY = super.getCenter().getY();
	int width = super.getWidth();

	// If there's a move
	if (currentX != super.getSquadron().getDestinationPlanet().getCenter().getX()
		|| currentY != super.getSquadron().getDestinationPlanet().getCenter().getY()) {
	    super.getShipMovement().moveTo();
	}

	// blink the ship if selected
	super.getShipSelectorManager().blinkSelection(graphics);

	graphics.setColor(Utils.getPlayerColor(super.getSquadron().getOwner().getId()));
	graphics.fillOval((int) currentX - width / 2, (int) currentY - width / 2, width, width);

	// Delete the squadron if arrived to its destination
	getShipMovement().impactDestination(graphics);
    }

    public boolean intersects(Shape s) {
	double radius = super.getWidth();
	return Point2D.distanceSq(s.getBounds().getCenterX(), s.getBounds().getCenterY(), getCenter().getX(), getCenter().getY()) <= radius
		* radius + radius * radius;
    }

    @Override
    public boolean intersect(GraphicItem item) {
	double radius = super.getWidth();
	return Point2D.distanceSq(item.getShape().getBounds().getCenterX(), item.getShape().getBounds().getCenterY(), getCenter()
		.getX(), getCenter().getY()) <= radius * radius + radius * radius;
    }

    @Override
    public boolean contains(Point2D p) {
	return this.shape.contains(p);
    }

    @Override
    public Shape getShape() {
	return shape;
    }
}
